package cn.tedu._05weibo.controller;

import cn.tedu._05weibo.common.response.JsonResult;
import cn.tedu._05weibo.common.response.StatusCode;
import cn.tedu._05weibo.mapper.WeiboMapper;
import cn.tedu._05weibo.pojo.dto.WeiboDTO;
import cn.tedu._05weibo.pojo.entity.Weibo;
import cn.tedu._05weibo.pojo.vo.UserVO;
import cn.tedu._05weibo.pojo.vo.WeiboDetailVO;
import cn.tedu._05weibo.pojo.vo.WeiboindexVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;
@Validated
@Api(tags = "02微博模块")
@RestController
@RequestMapping(value = "/v1/weibo/")
public class WeiboController {
    @Autowired
    private WeiboMapper weiboMapper;

    @ApiOperation("发布微博功能")
    @PostMapping(value = "insert")
    public JsonResult addWeibo(@RequestBody WeiboDTO weiboDTO, @ApiIgnore HttpSession session) {
        System.out.println(weiboDTO);
        UserVO uservo = (UserVO) session.getAttribute("user");
        if (uservo != null) {
            Weibo weibo = new Weibo();
            BeanUtils.copyProperties(weiboDTO, weibo);
            weibo.setDate(new Date());
            weibo.setUserId(uservo.getId());
            if (weiboMapper.insert(weibo) > 0) {
                return JsonResult.ok();
            }
        }
        return new JsonResult(StatusCode.NOT_LOGIN);
    }

    @ApiOperation("微博首页")
    @GetMapping(value = "selectIndex")
    public JsonResult weiboIndex() {
        return JsonResult.ok(weiboMapper.selectIndex());
    }

    @ApiImplicitParam(value = "微博ID",required = true,name = "id",dataType = "Long")
    @ApiOperation("微博详情页")
    @GetMapping(value = "selectById")
    public JsonResult selectDetailId(@Range(min = 200,max = 300,message = "微博ID必须在200-300之间") @RequestParam("id") Long id) {
        if (id<0){
            throw new IllegalArgumentException("ID值不能小于0");
        }

        return JsonResult.ok(weiboMapper.selectById(id));
    }

}
